#coding:utf-8
import xmind
import argparse
from xmindparser import xmind_to_dict
import os
data=xmind_to_dict("E:\\QITA\\root节点.xmind")
data=data[0].get("topic").get("topics")
print(data)

# print(os.getcwd())
'''parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, dest='inputfile', default="E:\\QITA\\root节点.xmind",
                    help='Default inputfile is 思维导图.xmind')
#parser.add_argument('-o', '--output-file', type=str, dest='outputfile', default='测试案例.xls',
  #                  help='Default outputfile is 测试案例.xls')
args = parser.parse_args()

workbook = xmind.load("E:\\QITA\\root节点.xmind")
sheet = workbook.getPrimarySheet()
# root_topic = sheet.getRootTopic()
root = sheet.getData()
print(root)'''

'''from xmindparser import xmind_to_dict
import xmind
import sys
import xlwt
import argparse
import os
import time

# 设置从第n+1行开始插入excel
row_num = 1
# 设置用例名称所在列(0为第一列)
name_col_num = 2
# 设置前置条件所在列、及其对应图标
condition_col_num = 3
condition_markers = 'symbol-info'
# 设置操作步骤所在列、及其对应图标
steps_col_num = 4
steps_markers = 'c_symbol_pen'
# 设置期望结果所在列、及其对应图标
expect_col_num = 5
# 设置用例类型所在列、用例类型
type_col_num = 8
testcase_type = '功能测试'
# 设置用例“适用阶段”所在列、以及类型
stage_col_num = 9
testcase2_type = '系统测试阶段'
# 设置用户优先级所在列
priority_col_num = 7


# 用于设置插入excel的标题
def set_excel_header():
    n = 0
    header = ['所属模块', '用例编号', '用例标题', '前置条件', '步骤', '预期', '关键词', '优先级', '用例类型', '适用阶段', '是否自动化', '自动化平台', '自动化用例路径']
    for i in header:
        ws.write(0, n, i)
        n += 1


# 该方法用于插入excel
def inser_excel(text, row, col):
    ws.write(row, col, text)


# 该方法用于生成用例名称,格式为模块1_模块2_模块3_模块4_……，
def per_round(element, str):
    global row_num
    if element['markers'].count(condition_markers) != 0:
        # 判断当前是否为前置条件
        # print("这是前置条件")
        condition_desc = element['title']
        inser_excel(condition_desc, row_num, condition_col_num)
    elif element['markers'].count(steps_markers) != 0:
        # 判断当前是否为操作步骤
        # print("这是操作步骤")
        steps_desc = element['title']
        inser_excel(steps_desc, row_num, steps_col_num)
    else:
        str += '_' + element['title']

    for child in element['topics']:
        per_fun(child, str)
        value = child['title']
        # print('--------',value)
        # print(child)
        #xmind结构，==7代表最后一个节点 {'id': 'cf397084617f4700c721e66667', 'link': None, 'title': '1.42', 'note': None, 'label': None,'comment': None, 'markers': []}
        if len(child) == 7:
            inser_excel(str, row_num, name_col_num)
            inser_excel(testcase_type, row_num, type_col_num)
            inser_excel(testcase2_type, row_num, stage_col_num)
            row_num += 1


def per_fun(element, str):
    global row_num
    if len(element) == 7:
        value = element['title']
        # print(row_num,':',value)
        # 将思维导图中最后一列设置为期望结果，并写入excel
        inser_excel(value, row_num, expect_col_num)
        # 开始设置优先级
        if len(element['markers']) != 0:
            priority = element['markers'][0]
            if priority == 'priority-1':
                inser_excel(1, row_num, priority_col_num)
            elif priority == 'priority-2':
                inser_excel(2, row_num, priority_col_num)
            elif priority == 'priority-3':
                inser_excel(3, row_num, priority_col_num)
            elif priority == 'priority-4':
                inser_excel(4, row_num, priority_col_num)
            else:
                inser_excel(4, row_num, priority_col_num)
        else:
            inser_excel(3, row_num, priority_col_num)

    else:
        # 将思维导图中除最后一列外，以"_"间隔组成用例名称
        per_round(element, str)


# 重新设置生成文件名
def set_file_name(input_filename, output_filename):
    if output_filename == '测试案例.xls':
        str = input_filename.replace('.xmind', '_') + output_filename
        path2 = os.getcwd() + '\\' + str
        if os.path.exists(path2):
            time1 = time.strftime("%Y%m%d%H%M%S", time.localtime())
            str = input_filename.replace('.xmind', '_') + '测试案例' + time1 + '.xls'
        return str
    else:
        path3 = os.getcwd() + '\\' + output_filename
        if os.path.exists(path3):
            print('检测到当前路径下已有存在文件：', output_filename, '\n是否继续覆盖并生成文件？Y:继续')
            isg = input()
            if isg == 'Y' or isg == 'y':
                os.remove(path3)
            else:
                print(output_filename, '文件生成操作已取消。')
                sys.exit()
        return output_filename


parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, dest='inputfile', default='思维导图.xmind',
                    help='Default inputfile is 思维导图.xmind')
parser.add_argument('-o', '--output-file', type=str, dest='outputfile', default='测试案例.xls',
                    help='Default outputfile is 测试案例.xls')
args = parser.parse_args()

if args.inputfile is None:
    parser.print_help()
    exit()

path3 = os.getcwd() + '\\' + args.inputfile
if not os.path.exists(path3):
    print('\n文件\"' + path3 + '\"不存在，请确认后再次操作，谢谢！')
    exit()

file_name = set_file_name(args.inputfile, args.outputfile)

print('开始将', args.inputfile, '文件转换为excel。。。')

workbook = xmind_to_dict(args.inputfile)

root=workbook[0].get("topic").get("topics")
# print(root)

str = ''
sheet = xlwt.Workbook()
ws = sheet.add_sheet('用例导入模板', cell_overwrite_ok=True)
set_excel_header()

per_round(root, str)

sheet.save(file_name)
print('导出完成，已生成文件：', file_name, '，总计', row_num - 1, '条用例。')'''
